﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.VisualStudio.VCProjectEngine;
using EnvDTE;

using iMainAddin;
using iSystem;
using System.Windows.Forms;

namespace iClassAddin
{
    class iVcClassProject : iVcProject
    {
        public override bool Init()
        {
            return true;
        }
        public override bool Create(Object createValue)
        {
            try
            {
                base.Create(createValue);

                iCreateClassData createData = createValue as iCreateClassData;
                if (createData == null)
                    return false;


                Solution CurrentSolution = (Solution)Application.Solution;
                SelectedItems selectItems = Application.SelectedItems;
                SelectedItem selectItem = selectItems.Item(1);
                SetVCProject(ref selectItem);
                if (VCProjcet == null)
                    return false;

                String addfilePath = VCProjcet.ProjectDirectory;
                if (!iFileAssist.RemoveTail(ref addfilePath, @"Win32\VC90"))
                    return false;

                String classDataPath = AddintDataPath + "Class\\";
                String itemFullPath = null;
                String tempFile = null;

                List<String> itemList = new List<String>(3);
                if (createData.isInline)
                {
                    itemFullPath = addfilePath + createData.inlineName;
                    tempFile = classDataPath + "inline.inl";
                    if (File.Exists(tempFile))
                    {
                        File.Copy(tempFile, itemFullPath);
                        itemList.Add(itemFullPath);
                    }
                }

                if (createData.isMakeCpp)
                {
                    itemFullPath = addfilePath + createData.cppName;
                    tempFile = classDataPath + "cpp.cpp";
                    if (File.Exists(tempFile))
                    {
                        File.Copy(classDataPath + "cpp.cpp", itemFullPath);
                        itemList.Add(itemFullPath);
                    }
                }

                itemFullPath = addfilePath + createData.headerName;
                tempFile = classDataPath + "header.h";
                if (File.Exists(tempFile))
                {
                    File.Copy(classDataPath + "header.h", itemFullPath);
                    itemList.Add(itemFullPath);
                }

                iClassDocument changeFile = new iClassDocument();
                String fileName = iSystem.iFileAssist.RemoveExtension(createData.headerName);

                changeFile.AddDictionary("$(ClassName)", createData.className);
                changeFile.AddDictionary("$(ProjectName)", VCProjcet.Name);
                changeFile.AddDictionary("$(ProjectNameUP)", VCProjcet.Name.ToUpper());
                changeFile.AddDictionary("$(FileName)", fileName);
                changeFile.AddDictionary("$(FileNameUP)", fileName.ToUpper());

                if (selectItem.ProjectItem != null && selectItem.ProjectItem.Kind == "{6BB5F8F0-4483-11D3-8BCF-00C04F8EC28C}")
                {
                    VCFilter filter = selectItem.ProjectItem.Object as VCFilter;
                    foreach (String itemPath in itemList)
                    {
                        changeFile.StartChange(itemPath, createData);
                        filter.AddFile(itemPath);
                    }
                }
                else
                {
                    foreach (String itemPath in itemList)
                    {
                        changeFile.StartChange(itemPath, createData);
                        selectItem.Project.ProjectItems.AddFromFile(itemPath);
                    }
                }
                VCProjcet.Save();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("ERROR: " +ex.Message);
            }
            return true;
        }
    }
}
